package com.xiaodou.mjf.application.core.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

import com.xiaodou.mjf.application.core.repository.OrderRepository;
import com.xiaodou.mjf.application.core.service.OrderService;
import com.xiaodou.mjf.entity.core.Order;
import com.xiaodou.mjf.infrastructure.common.app.JpaSimpleService;
import com.xiaodou.mjf.infrastructure.common.utils.StringUtils;

/***
 *
 * Order业务访问实现
 * 
 * @author XiaoDou.com Code Generator
 * @version 1.0 2014-10-29 17:37:52
 *
 */
@Service
@Transactional
public class OrderServiceImpl extends JpaSimpleService<Order, String> implements OrderService {

	Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class);

	@Resource
	OrderRepository orderRepository;

	@Override
	protected JpaRepository<Order, String> getRepository() {
		return orderRepository;
	}

	@Override
	public Order get(String extOrderId, Integer paymentStatus) {

		return orderRepository.findOne((root, query, cb) -> {
			Predicate p1 = cb.equal(root.get("status").as(Integer.class), Order.NORMAL);
			Predicate p2 = cb.equal(root.get("extOrderId").as(String.class), extOrderId);
			Predicate p3 = cb.equal(root.get("paymentStatus").as(Integer.class), paymentStatus);
			return query.where(p1, p2, p3).getRestriction();
		});

	}

	@Override
	public Page<Order> page(Order entity) {
		return orderRepository.findAll((root, query, cb) -> {
			Predicate p1 = cb.equal(root.get("paymentStatus").as(Integer.class), entity.getPaymentStatus());
			Predicate p2 = cb.like(root.get("merchantId").as(String.class), "%" + entity.getMerchantId() + "%");
			Predicate p3 = cb.like(root.get("channelId").as(String.class), "%" + entity.getChannelId() + "%");
			Predicate p4 = cb.like(root.get("serialNumber").as(String.class), "%" + entity.getChannelId() + "%");

			SimpleDateFormat spf = new SimpleDateFormat("yyyy-MM-dd");
			String startStr = "2000-01-01";
			String endStr = "2300-01-01";
			Date startTime = entity.getStartTime();
			Date endTime = entity.getEndTime();
			try {
				if (startTime == null)
					startTime = spf.parse(startStr);
				if (endTime == null)
					endTime = spf.parse(endStr);

			} catch (Exception e) {
				logger.error(e.toString());
				e.printStackTrace();
			}

			Predicate p5 = cb.between(root.get("txnDate").as(Date.class), startTime, endTime);
			// 美疯啦
				Predicate p6 = cb.equal(root.get("channelId").as(String.class), "10210001");
				// 众筹
				Predicate p7 = cb.equal(root.get("merchantId").as(String.class), "MFL_CROWD_20210001");
				Predicate p8 = cb.or(p6, p7);

				if (entity.getPaymentStatus().equals(-1)) {
					if (StringUtils.isNotBlank(entity.getMerchantId()) && StringUtils.isNotBlank(entity.getChannelId())) {
						return query.where(p2, p3, p5, p8).getRestriction();
					} else if (StringUtils.isNotBlank(entity.getMerchantId()) && StringUtils.isBlank(entity.getChannelId())) {
						return query.where(p2, p5, p8).getRestriction();
					} else if (StringUtils.isBlank(entity.getMerchantId()) && StringUtils.isNotBlank(entity.getChannelId())) {
						return query.where(p3, p5, p8).getRestriction();
					}

				} else {
					if (StringUtils.isNotBlank(entity.getMerchantId()) && StringUtils.isNotBlank(entity.getChannelId())) {

						return query.where(p1, p2, p3, p5, p8).getRestriction();
					} else if (StringUtils.isNotBlank(entity.getMerchantId()) && StringUtils.isBlank(entity.getChannelId())) {
						return query.where(p1, p2, p5, p8).getRestriction();
					} else if (StringUtils.isBlank(entity.getMerchantId()) && StringUtils.isNotBlank(entity.getChannelId())) {
						return query.where(p1, p3, p5, p8).getRestriction();
					} else if (StringUtils.isBlank(entity.getMerchantId()) && StringUtils.isBlank(entity.getChannelId())) {
						return query.where(p1, p5, p8).getRestriction();
					}
				}
				return query.where(p4, p5, p8).getRestriction();
			}, preparePage(entity, new Sort(new Sort.Order(Sort.Direction.DESC, "merchantId"), new Sort.Order(Sort.Direction.DESC, "txnDate"))));
	}

	@Override
	public List<Order> getOrderDayOfWeek(Date beginDate, Date endDate) {
		return orderRepository.getOrderDayOfWeek(beginDate, endDate);
	}
}
